package com.meixiang.fragment.shopping;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.shopping.GoodsDetailActivity;
import com.meixiang.adapter.shopping.GoodsParamsAdapter;
import com.meixiang.entity.shopping.GoodsParameterEntity;
import com.meixiang.entity.shopping.result.GoodsParameterResult;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.util.AbJsonUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 商品参数
 *
 * @author LZRUI
 */
public class GoodsParamsFragment extends BaseFragment {

    @Bind(R.id.swipe_target)
    RecyclerView ryParams;
    @Bind(R.id.tv_hint)
    TextView tvHint;

    private static String GOODS_ID = "goods_id";
    private String goodsId;

    private GoodsParamsAdapter adapter;
    private List<GoodsParameterEntity> dataList;

    public GoodsParamsFragment() {

    }

    public static GoodsParamsFragment newInstance(String goodsId) {
        GoodsParamsFragment fragment = new GoodsParamsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(GOODS_ID, goodsId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            goodsId = getArguments().getString(GOODS_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_goods_params, container, false);
        ButterKnife.bind(this, rootView);
        super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        ryParams.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new GoodsParamsAdapter(activity);
        ryParams.setAdapter(adapter);
    }

    @Override
    protected void setData() {
        HttpParams params = new HttpParams();
        params.put("goodsId", goodsId);

        Log.i("lzrtest", "商品参数：params " + params);

        HttpUtils.post(Config.GOODS_PARAMETER, GoodsDetailActivity.TAG, params, new HttpCallBack(activity) {

            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                GoodsParameterResult parameterResult = AbJsonUtil.fromJson(result.toString(), GoodsParameterResult.class);
                dataList = new ArrayList<>();
                // 不显示商品编号
//                if (!TextUtils.isEmpty(parameterResult.getGoodsSerial())) {
//                    dataList.add(new GoodsParameterEntity("商品编号", parameterResult.getGoodsSerial()));
//                }
                if (!TextUtils.isEmpty(parameterResult.getGoodsName())) {
                    dataList.add(new GoodsParameterEntity("商品名称", parameterResult.getGoodsName()));
                }
                dataList.addAll(parameterResult.getGoodsParameterList());
                adapter.setDatas(dataList);
            }

            @Override
            public void onError(String code, String message) {

            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                if (adapter.getItemCount() == 0) {
                    if (ryParams != null && tvHint != null) {
                        ryParams.setVisibility(View.GONE);
                        tvHint.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (ryParams != null && tvHint != null) {
                        ryParams.setVisibility(View.VISIBLE);
                        tvHint.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
